<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>晴天医诊</title>
    <meta name="description" content="晴天医诊">
    <meta name="keywords" content="晴天医诊">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <!-- 引入权限拦截器interceptor.js 一定要在axios后面 -->
    <script src="../plugins/interceptor.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>

</head>

<body class="hold-transition">
<div id="app">
    <!--面包屑头部-->
    <div class="content-header">
        <h1>用户管理</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <!-- 查询、新建 -->
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;" class="filter-item"
                          @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <!--表格头部-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '1' ? '男' : scope.row.gender == '2' ? '女' : '未知'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="生日" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>

                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.station == '0' ? '在职' : scope.row.station == '1' ? '休假' : '未知'}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--表格内容-->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="用户信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="未知" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <template>
                                                    <div class="block">
                                                        <span class="demonstration"></span>
                                                        <el-date-picker
                                                                v-model="formData.birthday"
                                                                type="date"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remark"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="formData.station">
                                                    <el-option label="在职" value="0"></el-option>
                                                    <el-option label="休假" value="1"></el-option>
                                                    <el-option label="未知" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键字</th>
                                            <th>角色备注</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <!--tableData 所有的检查项-->
                                        <tr v-for="c in tableData">
                                            <td>
                                                <!--v-model="checkitemIds" 用户勾选的id会自动进入这个属性-->
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="用户信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="未知" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="生日">
                                                <template>
                                                    <div class="block">
                                                        <span class="demonstration"></span>
                                                        <el-date-picker
                                                                v-model="formData.birthday"
                                                                type="date"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remark"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="formData.station">
                                                    <el-option label="在职" value="0"></el-option>
                                                    <el-option label="休假" value="1"></el-option>
                                                    <el-option label="未知" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键字</th>
                                            <th>角色备注</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <!--tableData 所有的检查项-->
                                        <tr v-for="c in tableData">
                                            <td>
                                                <!--v-model="checkitemIds" 用户勾选的id会自动进入这个属性-->
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click=" handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({


        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,//当前页
                pageSize: 10, //页面大小
                total: 100, //数据总数
                queryString: null, //搜索关键字
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false//控制编辑窗口显示/隐藏
        },

        //钩子函数
        created() {
            this.findPage();
        },

        methods: {

            //编辑
            handleEdit() {
                //进行数据保存
                // 把用户勾选的检查项id集合和检查组的基本信息打包一起提交
                axios.post("/user/update.do?roleIds=" + this.roleIds, this.formData).then((res) => {
                    // 6、关闭弹框，刷新页面，提示用户
                    if (res.data.flag) {
                        // 关闭弹框,刷新页面
                        this.dialogFormVisible4Edit = false;
                        this.findPage();
                    }
                    //提示用户,只是提示的样式不一样

                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : "error"
                    });
                })
            },

            //添加
            handleAdd() {
                //检查组的基本信息在formData里面
                //用户勾选的检查项id集合 checkitemIds
                //我们最好把id集合和formData打包一起传
                //在后端接收参数的pojo里面加上一个属性 List<Integer> checkitemIds

                axios.post("/user/add.do?roleIds=" + this.roleIds, this.formData).then((res) => {

                    //不管成功失败都要提示用户，只是提示的样式不一样
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : "error"
                    })
                    if (res.data.flag) {
                        //成功关闭弹框
                        this.dialogFormVisible = false;
                        // 刷新页面
                        this.findPage();
                    }
                });

            },

            //分页查询
            findPage() {
                //传递 当前页码 ，每页显示条数，查询条件
                axios.post("/user/findPage.do", this.pagination).then((res) => {
                    //6、返回总记录数给分页组件，分页查询的结果给表格
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.rows;
                    console.log(res.data);
                })
            },

            // 重置表单
            resetForm() {
                this.roleIds = [];
                //选中基本信息标签项
                this.activeName = 'first'
                this.formData = {};
            },

            // 弹出添加窗口
            handleCreate() {
                // 清空表单数据
                this.resetForm();
                //查询所有检查项
                axios.get("/user/findAllRole.do").then((res) => {
                    // res.data = {flag,message,data}
                    this.tableData = res.data.data;
                    //弹出新增框
                })
                this.dialogFormVisible = true;
            },

            /*            // 弹出编辑窗口
                        handleUpdate(row) {
                            //2、在编辑事件里面回显数据
                            //检查组基本信息  根据id获到检查组基本信息  formData
                            //所有的检查项信息  利用表格使用v-for显示所有的检查项  tableData
                            //用户勾选的检查项id集合  checkitemIds
                            //axios.get("/checkgroup/findById4Edit.d?id=" + row.id)
                            axios.get("/user/findById4Edit.do?id=" + row.id).then((res) => {
                                console.log(res.data.data);
                                this.roleIds = res.data.data.roleIds;//给checkitemIds
                                this.tableData = res.data.data.roles; //给tableData
                                this.formData = res.data.data.user;//给formData
                                this.dialogFormVisible4Edit = true;
                            });

                        },*/

            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                // 弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                // 获取检查组的id
                var id = row.id;
                axios.get("/user/findById.do?id=" + id).then(res => {
                    if (res.data.flag) {
                        // 成功绑定数据
                        this.formData = res.data.data;
                        //发送请求后台获取所有检查项数据，得到后绑定tableData(检查项列表)
                        axios.get('/user/findAllRole.do').then(resp => {
                            if (resp.data.flag) {
                                this.tableData = resp.data.data;
                                // 获取选中的检查项id, 回显数据时，检查项列表要钩选中
                                axios.get("/user/findRoelIdsByUserId.do?userId=" + id).then(response => {
                                    // 后台要返回data必须为List
                                    if (response.data.flag) {
                                        this.roleIds = response.data.data;
                                    } else {
                                        this.$message.error(response.data.message);
                                    }
                                })
                            } else {
                                this.$message.error(resp.data.message);
                            }
                        })
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            // 删除
            handleDelete(row) {

                axios.get("/user/delete.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        // 刷新页面
                        this.findPage();
                    }
                    //不管成功失败都要提示用户，只是提示的样式不一样
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : "error"
                    });
                });
            }
        }

    })
</script>
</html>
